package com.zhou.org.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zhou.framework.util.SettingUtil;
import com.zhou.util.StringTool;
import com.zhou.org.dao.Setting;
import com.zhou.org.dto.SettingQueryDto;
import com.zhou.org.mapper.SettingMapper;
import com.zhou.org.service.SettingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lang.zhou
 * @since 2022-08-23
 */
@Service
public class SettingServiceImpl extends ServiceImpl<SettingMapper, Setting> implements SettingService {
    @Override
    public List<Setting> list(SettingQueryDto dto) {
        LambdaQueryWrapper<Setting> query = new LambdaQueryWrapper<>();
        if(StringTool.isNotBlank(dto.getModule())){
            query.like(Setting::getModule,dto.getModule()+"%");
        }
        if(StringTool.isNotBlank(dto.getName())){
            query.like(Setting::getName,"%" + dto.getName()+"%");
        }
        query.orderByAsc(Setting::getModule);
        query.orderByAsc(Setting::getName);
        List<Setting> list = this.list(query);
        /*if(list.size() > 0){
            for (Setting setting : list) {
                SettingUtil.decode(setting);
            }
        }*/
        return list;
    }

    @Override
    public Setting getSetting(String module, String name) {
        LambdaQueryWrapper<Setting> w = new LambdaQueryWrapper<>();
        w.eq(Setting::getModule,module).eq(Setting::getName,name);
        Setting setting = baseMapper.selectOne(w);
        //return SettingUtil.decode(setting);
        return setting;
    }

    @Override
    public void saveSetting(Setting setting) {
        //SettingUtil.encode(setting);
        Setting old = this.getSetting(setting.getModule(),setting.getName());
        if(old == null){
            SettingUtil.encode(setting);
            this.save(setting);
        }else{
            if(!Objects.equals(setting.getValue(),old.getValue())){
                //如果修改过参数，则进行一次加密
                SettingUtil.encode(setting);
            }
            baseMapper.updateSetting(setting);
        }
        SettingUtil.putSetting(setting);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateValue(String module, String name, Object value) {
        LambdaUpdateWrapper<Setting> w = new LambdaUpdateWrapper<>();
        w.set(Setting::getValue,value);
        w.eq(Setting::getModule,module);
        w.eq(Setting::getName,name);
        update(w);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateValue(String module, Map<String, Object> map) {
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            this.updateValue(module,entry.getKey(),StringTool.safeToString(entry.getValue(),null));
        }
        SettingUtil.refreshCache();
    }

    @Override
    public List<String> getSettingModule() {
        return baseMapper.getSettingModule();
    }
}
